package com.szcinda.rpa.controller;

import com.szcinda.rpa.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("app")
public class AppController {


    @GetMapping("checkVersion")
    public Result<Integer> checkVersion(){
        return Result.success(2);
    }

    @GetMapping("download/{versionCode}")
    public void download(@PathVariable String versionCode, HttpServletResponse response) throws Exception{
        // 发送给客户端的数据
        try (OutputStream outputStream = response.getOutputStream()) {
            // 设置编码
            String encodingFileName = new String("app.apk".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setHeader("Content-Disposition", "attachment;filename=" + encodingFileName);
            byte[] buff = new byte[1024];
            BufferedInputStream bis;
            // 读取filename
            bis = new BufferedInputStream(new FileInputStream(new File("/usr/local/rpa-backend/file/app.apk")));
            int i = bis.read(buff);
            while (i != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                i = bis.read(buff);
            }
        }
    }
}
